<?php

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

$sid = intval($_GET['sid']);

if($_GET['op'] == 'delete') {
	if(submitcheck('deletesubmit')) {
		include_once(S_ROOT.'./source/function_delete.php');
		deleteshares(array($sid));
		showmessage('do_success', $_GET['type']=='view'?'space.php?do=share':$_POST['refer'], 0);
	}
} elseif($_GET['op'] == 'edithot') {
	//权限
	if(!checkperm('manageshare')) {
		showmessage('no_privilege');
	}
	
	if($sid) {
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('share')." WHERE sid='$sid'");
		if(!$share = $_SGLOBAL['db']->fetch_array($query)) {
			showmessage('no_privilege');
		}
	}
		
	if(submitcheck('hotsubmit')) {
		$_POST['hot'] = intval($_POST['hot']);
		updatetable('share', array('hot'=>$_POST['hot']), array('sid'=>$sid));
		if($_POST['hot']>0) {
			include_once(S_ROOT.'./source/function_feed.php');
			feed_publish($sid, 'sid');
		} else {
			updatetable('feed', array('hot'=>$_POST['hot']), array('id'=>$sid, 'idtype'=>'sid'));
		}
		
		showmessage('do_success', $_POST['refer'], 0);
	}
	
} else {

	if(!checkperm('allowshare')) {
		ckspacelog();
		showmessage('no_privilege');
	}
	//实名认证
	ckrealname('share');
	
	//视频认证
	ckvideophoto('share');

	//新用户见习
	cknewuser();

	$type = empty($_GET['type'])?'':$_GET['type'];
	$pid = empty($_GET['pid'])?0:intval($_GET['pid']);
	$pidstr = empty($_GET['planids'])?'':trim($_GET['planids']);

	$query = $_SGLOBAL['db']->query("select uid from ".tname('shareplan')." where id = ".$pid);
	if($value = $_SGLOBAL['db']->fetch_array($query)) {
		if($value['uid'] == $_SGLOBAL['supe_uid']){
			showmessage('is_shared', $_POST['refer'], 0);
		}
	}
	//添加分享
	if(submitcheck('sharesubmit')){

		/*记录分享用户的信息*/ 
		$parr['uid']= $_SGLOBAL['supe_uid'];
		$pid = empty($_POST['pid'])?0:intval($_POST['pid']);
		$share_title_str = $first_share = '';
		$note_uid = 0;

		if($_POST['pid'] == '0'){
			$parr['fromuid'] = 0;
			$parr['sourceuid'] = $_SGLOBAL['supe_uid'];
			$parr['planid'] = implode(',', explode('_',$_POST['pids']));
		}else{
			$query = $_SGLOBAL['db']->query("select * from ".tname('shareplan')." where id = ".$_POST['pid']);
			if($value = $_SGLOBAL['db']->fetch_array($query)) {
				if($value['uid'] == $_SGLOBAL['supe_uid']){
					showmessage('is_shared', $_POST['refer'], 0);
				}else{
					$parr['fromuid'] = $note_uid = $value['uid'];
					$parr['sourceuid'] = $first_share = $value['sourceuid'];
					$parr['planid'] = $value['planid'];
					if($value['sourceuid'] == $value['fromuid'] && $value['fromuid']!='0'){
						$share_title_str = "<a href=\"space.php?uid=".$value['fromuid']."\">".$_SN[$value['fromuid']]."</a>".cplang('share_plan2');
					}else if($value['sourceuid'] != $value['fromuid'] && $value['fromuid']!='0'){
						$share_title_str = "<a href=\"space.php?uid=".$value['uid']."\">".$_SN[$value['uid']]."</a>".cplang('share_plan4')."<a href=\"space.php?uid=".$value['fromuid']."\">".$_SN[$value['fromuid']]."</a>".cplang('share_plan2');
					}
				}
			}
		}

		$spid = inserttable('shareplan',$parr,1);

		$note_message = cplang('share_plan5')."<a href=\"space.php?do=findship&pid=$spid\">".implode(',',$shipenames).cplang('share_plan3')."</a>";

		$planshipids = explode(',',$parr['planid']);
		$shipenames = array();
		foreach($planshipids as $v){
			$query = $_SGLOBAL['db']->query("select shipename from ".tname('plan')." where id = ".$v);
			if($value = $_SGLOBAL['db']->fetch_array($query)) {
				$shipenames[] = $value['shipename'];
			}
		}
		if(empty($_POST['refer'])) $_POST['refer'] = "space.php?do=share&view=me";
		$arr['title_template'] = cplang('share_plan1').$share_title_str."<a href=\"space.php?do=findship&pid=$spid\">".implode(',',$shipenames).cplang('share_plan3')."</a>";
		if($first_share != ''){
			$arr['body_template'] = cplang('share_plan6')." {username} ".cplang('share_plan4');
			$arr['body_data'] = serialize(array(
				'username' => "<a href=\"space.php?uid=$first_share\">".$_SN[$first_share]."</a>"
			));
		}
		
		$arr['body_general'] = getstr($_POST['general'], 150, 1, 1, 1, 1);
		$arr['type'] = $type;
		$arr['uid'] = $_SGLOBAL['supe_uid'];
		$arr['username'] = $_SGLOBAL['supe_username'];
		$arr['dateline'] = $_SGLOBAL['timestamp'];
		$arr['friend'] = $_POST['friend'];

		//入库
		$setarr = saddslashes($arr);//增加转义
		$sid = inserttable('share', $setarr, 1);

		//统计
		updatestat('share');

		//被分享通知当事人
		if($note_uid && $note_uid != $_SGLOBAL['supe_uid']) {
			notification_add($note_uid, 'sharenotice', $note_message);
		}

		//更新用户统计
		if(empty($space['sharenum'])) {
			$space['sharenum'] = getcount('share', array('uid'=>$space['uid']));
			$sharenumsql = "sharenum=".$space['sharenum'];
		} else {
			$sharenumsql = 'sharenum=sharenum+1';
		}

		//动态
		if(ckprivacy('share', 1)) {
			include_once(S_ROOT.'./source/function_feed.php');
			feed_publish($sid, 'sid', 1);
		}

		/*if($_POST['topicid']) {
			topic_join($_POST['topicid'], $_SGLOBAL['supe_uid'], $_SGLOBAL['supe_username']);
			$url = 'space.php?do=topic&topicid='.$_POST['topicid'].'&view=share';
		} else {
			$url = $_POST['refer'];
		}*/

		showmessage('do_success', $_POST['refer'], 0);
	}

	//显示
	/*
	if(empty($pid)){
		$sourceuid = $_SGLOBAL['supe_uid'];
		$fromuid = 0;
	}
	$note_uid = 0;
	$note_message = '';
	$hotarr = array();
	$arr = array();
	$arr['body_data'] = serialize($arr['body_data']);//数组转化
	$arr = mkshare($arr);

	realname_get();*/
}

include template('cp_planshare');

?>